package com.rd.student.jike.class2.lihongming.Test;

import java.util.*;

/**
 * @Auther:Heinrich
 * @Date:2023/7/10-07-10-14:10
 * @Description:com.rd.student.jike.class2.lihongming
 * @version:1.0
 */
public class LinkedListTest {
    public static void main(String[] args) {
        LinkedList list = new LinkedList();
        Student1 LiHongMing = new Student1("李宏鸣", "计科", "2班", "", 80);
        Student1 XueZhiXin = new Student1("薛志鑫", "计科", "2班", "", 66);
        Student1 WangHaiXin = new Student1("王海鑫", "计科", "4班", "", 21);
        Student1 XiaYinYin = new Student1("夏银银", "计科", "4班", "", 18);
        Student1 WangXiangYang = new Student1("王向阳", "计科", "5班", "", 18);
        Student1 WuJunHao = new Student1("吴俊豪", "计科", "5班", "", 19);
        list.add(LiHongMing);
        list.add(XueZhiXin);
        list.add(WangHaiXin);
        list.add(XiaYinYin);
        list.add(WangXiangYang);
        list.add(WuJunHao);

        //排序测试
        System.out.println(list);
        Collections.sort(list);
        System.out.println("——————");
        System.out.println(list);
        System.out.println("——————");
        Student1.DownSort(list);
        System.out.println(list);



        Scanner scanner = new Scanner(System.in);

        while (true){
            System.out.println("请输入指令格式如：1");
            System.out.println("1.按班级查询学生列表");
            System.out.println("2.按专业查询学生列表");
            System.out.println("3.按年龄查询学生列表");
            System.out.println("4.查询年龄大于等于或小于21岁的学生列表");
            System.out.println("5.查询学生姓名列表");
            System.out.println("6，查询学生平均年龄");
            System.out.println("7.分组查询学生人数");
            System.out.println("8，退出系统");
            String ans = scanner.next();
            switch (ans){
                case "1":
                    System.out.println("请输入要查询的班级格式如:2班");

                    System.out.println(Student1.classify(list,scanner.next(),ans));
                    break;
                case "2":
                    System.out.println("请输入要查询的专业格式如:计科");
                    System.out.println(Student1.classify(list, scanner.next(),ans));
                    break;
                case "3":
                    System.out.println("请输入要查询的年龄格式如:18");
                    System.out.println(Student1.classify(list, scanner.next(),ans));
                    break;
                case "4":
                    System.out.println("请输入要查询的学生年龄筛选条件：（大于等于21输入1，小于21输入0）");

                    int ans3 = scanner.nextInt();
                    System.out.println(Student1.AgeChooseList(list,ans3));
                    break;
                case "5":
                    System.out.println(Student1.SelectName(list));
                    break;
                case "6":
                    Student1.AgeChooseList(list,-1899);
                    break;
                case "7":
                    System.out.println(Student1.groupBy(list,0));
                    System.out.println(Student1.groupBy(list,1));
                    System.out.println(Student1.groupBy(list,2));
                    break;
                case "8":
                    return;
                default:
                    System.out.println("指令有误！");
            }
        }













    }
}

class Student1 implements Comparable{
    public String name;
    public String dept;
    public String cla;
    public String comment;
    public int age;

    public Student1() {
    }

    public Student1(String name, String dept, String cla) {
        this.name = name;
        this.dept = dept;
        this.cla = cla;
    }

    public Student1(String name, String dept, String cla, String comment, int age) {
        this.name = name;
        this.dept = dept;
        this.cla = cla;
        this.comment = comment;
        this.age = age;
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student1 student1 = (Student1) o;
        return age == student1.age && Objects.equals(name, student1.name) && Objects.equals(dept, student1.dept) && Objects.equals(cla, student1.cla) && Objects.equals(comment, student1.comment);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, dept, cla, comment, age);
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", dept='" + dept + '\'' +
                ", cla='" + cla + '\'' +
                ", comment='" + comment + '\'' +
                ", age=" + age +
                '}'+"\n";
    }
//@Override
//public String toString() {
//    return this + "学号：" + "studentId" +
//            ",姓名：" + name +
//            ", 专业：" + dept +
//            ", 班级：" + cla +
//            ", 描述：" + comment +
//            ", 年龄：" + age +"\n";
//}

    /**
     *
     * @param student1List 学生列表
     * @param str 控制台接收
     * @param temp 控制台选择
     * @return
     */
    public static LinkedList classify(LinkedList<Student1> student1List, String str, String temp){

        LinkedList classifyList = new LinkedList();
        for (int i = 0; i < student1List.size(); i++) {
            if (student1List.get(i).cla.equals(str) && temp == "1") {
                classifyList.add(student1List.get(i));
            } else if (student1List.get(i).dept.equals(str) && temp == "2") {
                classifyList.add(student1List.get(i));
            } else if (student1List.get(i).age == Integer.valueOf(str) && temp == "3") {
                classifyList.add(student1List.get(i));
            }
        }
        return classifyList;
    }
//    @Deprecated
//    public static LinkedList DeptList(LinkedList StudentList,String dept){
//        Student s;
//        LinkedList deptList = new LinkedList();
//        for (int i = 0; i < StudentList.size(); i++) {
//            s = (Student) (StudentList.get(i));
//            if (s.dept.equals(dept)) {
//                deptList.add(s);
//            }
//        }
//        return deptList;
//    }
//
//    @Deprecated
//    public static LinkedList AgeList(LinkedList StudentList,int age){
//        Student s;
//        LinkedList ageList = new LinkedList();
//        for (int i = 0; i < StudentList.size(); i++) {
//            s = (Student) (StudentList.get(i));
//            if (s.age == age) {
//                ageList.add(s);
//            }
//        }
//        return ageList;
//    }
    public static LinkedList AgeChooseList(LinkedList StudentList,int age){
        Student1 s = null;
        int ave = 0;
        LinkedList AgeUpList = new LinkedList();
        LinkedList AgeDownList = new LinkedList();
        for (int i = 0; i < StudentList.size(); i++) {

            s = (Student1) (StudentList.get(i));
            ave += s.age;
            if (s.age >= 21 ) {
                AgeUpList.add(s);
            }else{
                AgeDownList.add(s);
            }
        }
        if(age == -1899)System.out.println("平均年龄为"+ave / StudentList.size());
        if(age == 0){
            return AgeDownList;
        }else{
            return AgeUpList;
        }

    }

    public static LinkedList SelectName(LinkedList list) {
        Student1 student1;
        LinkedList Names = new LinkedList();
        for (int i = 0; i < list.size(); i++) {
            student1 = (Student1) (list.get(i));
            Names.add(student1.name);
        }
        return Names;
    }

    public static HashMap groupBy(LinkedList<Student1> student1LinkedList, int temp) {
        HashMap hmp = new HashMap();
        int result;
        Object num;
        for (int i = 0; i < student1LinkedList.size(); i++) {
            if (temp == 0) {
                 num = hmp.get(student1LinkedList.get(i).cla);
            } else if (temp == 1) {
                 num = hmp.get(student1LinkedList.get(i).dept);
            }else{
                 num = hmp.get(student1LinkedList.get(i).age);
            }
            if (num == null) {
                result = 0;
            } else {
                result = (int) (num);
            }
            if (temp == 0) {
                hmp.put(student1LinkedList.get(i).cla, ++result);
            } else if (temp == 1) {
                hmp.put(student1LinkedList.get(i).dept, ++result);
            }else{
                hmp.put(student1LinkedList.get(i).age, ++result);
            }
        }
        return hmp;
    }


    @Override
    public int compareTo(Object o) {
        Student1 s = (Student1) o;
        if (this.age > s.age){
            return 1;
        } else if (this.age < s.age) {
            return -1;
        }
        return 0;
    }
    public static void DownSort(LinkedList list){
        //顺序排序
        Collections.sort(list);
        int i = 0,j = list.size()-1;
        Object o ;
        while (i<j){
            o = list.get(j);
            list.set(j,list.get(i));
            list.set(i,o);
            i++;
            j--;
        }
    }
}

